<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\api\controller\WXBizDataCrypt;
use fast\Http;

class Xcx extends Api
{

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    //微信开发者模式 相关参数
    private $AppId = 'wx68a30092900891bd';
    private $Secret = 'f175c01e3127c680f05e2ca255b75a7d';
    private $Code;
    private $EncryptedData;
    private $Iv;
    private $Openid = '';
    private $SessionKey;
    private $Unionid;

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 第一个接口
     */
    public function index(){

        $param = $this->request->request();

        $this->Code = $param['code'];
        $this->EncryptedData = $param['encryptedData'];
        $this->Iv = $param['iv'];

        $url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$this->AppId.'&secret='.$this->Secret.'&js_code='.$this->Code.'&grant_type=authorization_code';
        $result = Http::sendRequest($url);
        $result1 = json_decode($result['msg'], true);

        $this->Openid = $result1['openid'];
        $this->SessionKey = $result1['session_key'];

        $pc = new WXBizDataCrypt($this->AppId, $this->SessionKey);
        $errCode = $pc->decryptData($this->EncryptedData, $this->Iv);

        $this->success('发送成功', json_decode($errCode[1]), 200);
    
    }

}